package org.wangchaocheng.cloudgallery.authentication.strategy;

import java.util.Map;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.wangchaocheng.cloudgallery.common.exception.ErrorEnum;
import org.wangchaocheng.cloudgallery.common.exception.ExceptionFactory;
import org.wangchaocheng.cloudgallery.enums.user.UserAuthTypeEnum;

/**
 * 认证策略工厂
 *
 * @author wangchaocheng
 * @date 2025/04/22
 */
@Component
@RequiredArgsConstructor
public class AuthenticationStrategyFactory {

    private final Map<String, AuthenticationStrategy> authenticationStrategyMap;

    /**
     * 获取认证策略
     *
     * @param authType 认证类型
     * @return {@link AuthenticationStrategy}
     */
    public AuthenticationStrategy getAuthenticationStrategy(String authType) {
        if (!UserAuthTypeEnum.isSupport(authType)) {
            throw ExceptionFactory.bizException(ErrorEnum.E_PARAMETER_VALUE_NOT_IN_ARRAY, "server.message.error.authentication.un_support_auth_type");
        }
        String strategyName = UserAuthTypeEnum.valueOf(authType).getAuthStrategyBeanName();
        if (!authenticationStrategyMap.containsKey(strategyName)) {
            throw ExceptionFactory.bizException(ErrorEnum.E_PARAMETER_VALUE_NOT_IN_ARRAY, "server.message.error.authentication.un_support_auth_strategy");
        }
        return authenticationStrategyMap.get(strategyName);
    }
}
